<?php
namespace Cms\Controller;
use Cms\Controller\BaseController;
class CompanyController extends BaseController {

   /* public function _initialize() {
        //你可以在此覆盖父类方法
        parent::_initialize();
        $this->assign('useroath',$_SESSION['CMS']['user']['oath']);
       
    }*/
//公司列表
    public function companyList(){
        //设置面包导航，主加载器请配置
        $bread=array(
            '0'=>array(
                'name'=>'公司管理',
                'url'=>U('Cms/Company/companyList')
            )
        );
        $this->assign('breadhtml',$this->getBread($bread));
        //绑定搜索条件与分页
        $m=M('company',"t_");
        $p=$_GET['p']?$_GET['p']:1;
        $this->assign('p',$p);
        $search=I('search')?I('search'):'';

       // $map['is_del'] = 0;
        //搜索功能未做
        if($search){
            $map['carnum']=array('like',"%$search%");
            $this->assign('search',$search);
        }

        $psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
       // $cache=$m->join('t_locks as t on Ystech_car.lockid = t.ID','left')->field('Ystech_car.*,t.*')->where($map)->select();
        $cache=$m->where($map)->select();
        //dump($cache);
       // die;
        $count=$m->where($map)->count();
        $this->getPage($count, $psize, 'Ystech-loader', '公司列表','Ystech-search');
        $this->assign('cache',$cache);
        $this->assign('count',$count);
        $this->display();
    }
// 公司编辑
    public function companySet(){
        $id=I('id');
        $m=M('company','t_');
        //设置面包导航，主加载器请配置        
        $bread=array(
            '0'=>array(
                'name'=>'系统设置',
                'url'=>U('Cms/User/#')
            ),
            '1'=>array(
                'name'=>'公司列表',
                'url'=>U('Cms/Company/companyList')
            ),
            '1'=>array(
                'name'=>'公司编辑',
                'url'=>U('Cms/Company/companySet',array('id'=>$id))
            )
        );
        $this->assign('breadhtml',$this->getBread($bread));
        //处理POST提交  
        if(IS_POST){
            $data=I('post.');
            //$data['user_id']=$_SESSION['CMS']['uid'];
            //$data['user_name']=$_SESSION['CMS']['user']['username'];

             //var_dump($data);
            /*部门编号*/
            // if($data['duty_no']) {
            //     $wh = array('duty_no'=>$data['duty_no']);
            //     if($id) $wh['id'] = array('neq',$id);
            //     if($m->where($wh)->count()){
            //         $info['status']=0;
            //         $info['msg']='职位编号重复！';
            //         $this->ajaxReturn($info);
            //     }
            // }           
            
            if($id){
                unset($data['company']);
                $data['ModifyTime']=date("Y-m-d H:i:s");
                $re=$m->save($data);
                if(FALSE!==$re){
                    $info['status']=1;
                    $info['msg']='设置成功！';
                }else{
                    $info['status']=0;
                    $info['msg']='设置失败！';
                }
            }else{
                unset($data['id']);
                $data['CreateTime']=date("Y-m-d H:i:s");
                $re=$m->add($data);//echo D()->_sql();
                if($re){
                    $info['status']=1;
                    $info['msg']='设置成功！';
                }else{
                    $info['status']=0;
                    $info['msg']='设置失败！';
                }
            }
            $this->ajaxReturn($info);
        }
        
        //处理编辑界面
        if($id){            
            $cache=$m->where('id='.$id)->find();
            $this->assign('cache',$cache);  
        }
        
        $this->display();
    }


    //车辆设置
    public function cargoods(){
        $id=I('id');
        $m=M('car',"Ystech_");
        $l=M('locks',"t_");
        //设置面包导航，主加载器请配置
        $bread=array(
            '0'=>array(
                'name'=>'资源管理',
                'url'=>U('Cms/Locks/#')
            ),
            '1'=>array(
                'name'=>'智能锁列表',
                'url'=>U('Cms/Locks/goodsList')
            ),
            '2'=>array(
                'name'=>'智能锁编辑',
                'url'=>$id?U('Cms/Locks/goods',array('id'=>$id)):U('Cms/Locks/goods')
            )
        );
        /*锁信息记录*/
        $locks_list = $l->field('ID,SerialNo')->select();
        $this->assign('locks_list',$locks_list);
        $this->assign('breadhtml',$this->getBread($bread));
        //处理POST提交
        if(IS_POST){
            $data=I('post.');
            $id = $data['carid'];
            $date = date("Y-m-d H:i:s");
            if($id){
                $re=$m->where(array('carid'=>$id))->save($data);
                if(FALSE!==$re){
                    $info['status']=1;
                    $info['msg']='设置成功！';
                }else{
                    $info['status']=0;
                    $info['msg']='设置失败！';
                }
            }else{
                $re=$m->add($data);
                if($re){
                    $info['status']=1;
                    $info['msg']='设置成功！';
                }else{
                    $info['status']=0;
                    $info['msg']='设置失败！';
                }
            }
            $this->ajaxReturn($info);
        }
        //处理编辑界面
        if($id){
            $cache=$m->where('carid='.$id)->join('t_locks as t on Ystech_car.lockid = t.ID','left')->field('Ystech_car.*,t.*')->find();

            $this->assign('cache',$cache);
        }
        $this->display();
    }

    public function goodsDel(){
        $id=$_GET['id'];//必须使用get方法
        $m=M('car',"Ystech_");
        if(!$id){
            $info['status']=0;
            $info['msg']='ID不能为空!';
            $this->ajaxReturn($info);
        }
        $ids = explode(",",$id);
        $re=$m->where(array('carid'=>array('in',$ids)))->save(array('is_del'=>1));
        if($re){
            $info['status']=1;
            $info['msg']='删除成功!';
        }else{
            $info['status']=0;
            $info['msg']='删除失败!';
        }
        $this->ajaxReturn($info);
    }

    /**
     * 导出数据为excel表格
     *@param $data    一个二维数组,结构如同从数据库查出来的数组
     *@param $title   excel的第一行标题,一个数组,如果为空则没有标题
     *@param $filename 下载的文件名
     *@examlpe
    $stu = M ('User');
    $arr = $stu -> select();
    exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
     */
    private function exportexcel($data = array(), $title = array(), $filename = 'report') {
        header("Content-type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . $filename . ".xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        //导出xls 开始
        if (!empty($title)) {
            foreach ($title as $k => $v) {
                $title[$k] = iconv("UTF-8", "GB2312", $v);
            }
            $title = implode("\t", $title);
            echo "$title\n";
        }
        if (!empty($data)) {
            foreach ($data as $key => $val) {
                foreach ($val as $ck => $cv) {
                    $data[$key][$ck] = iconv("UTF-8", "GB2312", $cv);
                }
                $data[$key] = implode("\t", $data[$key]);
            }
            echo implode("\n", $data);
        }

    }

}
